package com.siwei.mes.sync.erp.service;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.entity.system.SupplierCompany;
import com.siwei.mes.service.materials.MaterialsService;
import com.siwei.mes.service.sync.erp.ErpSupplierCompanyMaterialsService;
import com.siwei.mes.service.system.SupplierCompanyMaterialsService;
import com.siwei.mes.service.system.SupplierCompanyService;
import com.siwei.mes.sync.erp.entity.SyncSupplierCompanyMaterials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP-供应商原材料相关实现类
 *
 * @Description: ERP-供应商原材料相关实现类
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/9/2 16:10
 */
@Service
public class SyncErpSupplierCompanyMaterialsService {
    private static final Logger log = LoggerFactory.getLogger("erp");
    @Resource
    private ErpSupplierCompanyMaterialsService erpSupplierCompanyMaterialsService;
    @Resource
    private SupplierCompanyService supplierCompanyService;
    @Resource
    private SupplierCompanyMaterialsService supplierCompanyMaterialsService;
    @Resource
    private MaterialsService materialsService;

    public void saveOrUpdate(SyncSupplierCompanyMaterials syncInfo) {
        log.info("正在处理ERP新增或修改供应商原材料，入参：{}", JSON.toJSONString(syncInfo));
        log.info("这里只处理修改供应商信息。");

        try {
            ///**第一步 先插入品控这边的扩展表 erp_supplier_company_materials*/
            //ErpSupplierCompanyMaterials entity = ErpSupplierCompanyMaterials.payloadInfo(syncInfo);
            //erpSupplierCompanyMaterialsService.syncSaveOrUpdate(entity);

            /**第二步 判断供应商是否存在  返回对应品控的供应商主键*/
            SupplierCompany supplierCompany = supplierCompanyService.saveOrUpdate(syncInfo.getYCID(), syncInfo.getYclCompanyName());

            //if (supplierCompany != null) {
            //    String category = syncInfo.getCategory();
            //    MaterialsEnum materialsEnum = MaterialsEnum.selectByErpType(category);
            //    if (materialsEnum == null) {
            //        log.info("ERP新增或修改供应商原材料，未配置此类型对照品控的类型，直接不做数据处理，对应类型：【{}】", category);
            //        return;
            //    }
            //
            //    /**判断ERP的物料 是否在品控中存在*/
            //    Materials materials = materialsService.checkIsExist(materialsEnum.getCode(), syncInfo.getYclName(), syncInfo.getPzgg());
            //    if (materials == null) {
            //        log.info("ERP新增或修改供应商原材料，原材料信息不存在，原材料类型：【{}】，原材料名称：【{}】，原材料规格：【{}】",
            //                materialsEnum.getCode(), syncInfo.getYclName(), syncInfo.getPzgg());
            //        return;
            //    }
            //
            //    /**第三步 新增供应商物料信息*/
            //    SupplierCompanyMaterials companyMaterials = new SupplierCompanyMaterials();
            //    companyMaterials.setSupplierCompanyId(supplierCompany.getId());
            //    companyMaterials.setMaterialsType(materialsEnum.getCode());
            //    companyMaterials.setMaterialsId(materials.getId());
            //    companyMaterials.setSpecId(materials.getSpecId());
            //
            //    companyMaterials.setMaterialsName(materials.getMaterialName());
            //    companyMaterials.setMaterialsSpec(materials.getMaterialSpecs());
            //    supplierCompanyMaterialsService.saveOrUpdate(companyMaterials);
            //}
        } catch (Exception e) {
            log.error("ERP新增或修改供应商原材料失败，错误原因：【{}】", e.getMessage(), e);
            /**手动回滚事务*/
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            assertTrue("ERP新增或修改供应商原材料失败，错误原因：" + e.getMessage() + "】");
        }
    }
}
